package io.renren.modules.rps.service.impl;

import io.renren.common.constant.Constant;
import io.renren.common.utils.ConvertUtils;
import io.renren.common.utils.Result;
import io.renren.modules.rps.dto.CenterDailyDTO;
import io.renren.modules.rps.dto.SiteDailyDTO;
import io.renren.modules.rps.service.CenterDailyService;
import io.renren.modules.rps.service.ReportAnalysisService;
import io.renren.modules.rps.service.SiteDailyService;
import io.renren.modules.security.user.SecurityUser;
import io.renren.modules.security.user.UserDetail;
import io.renren.modules.sys.enums.SuperAdminEnum;
import io.renren.modules.sys.service.SysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ReportAnalysisServiceImpl implements ReportAnalysisService {
    @Autowired
    private SiteDailyService siteDailyService;

    @Autowired
    private CenterDailyService centerDailyService;

    @Autowired
    private SysDeptService sysDeptService;

    @Override
    public List<SiteDailyDTO> list(Map<String, Object> params) {

        List<SiteDailyDTO> finalList = null;
        UserDetail user = SecurityUser.getUser();
        Long pidList = sysDeptService.getPid(user.getDeptId());
        String deptId = String.valueOf(params.get("deptId"));
        long id = 0l;
        if(!deptId.isEmpty()){
            id=Long.valueOf(deptId);
        }
        Long deptIdList = sysDeptService.getPid(id);

        if(user.getSuperAdmin() == SuperAdminEnum.YES.value()&&Constant.DEPT_ROOT.equals(deptIdList)){
            List<CenterDailyDTO> list = centerDailyService.list(params);
            finalList = ConvertUtils.sourceToTarget(list, SiteDailyDTO.class);
            return finalList;
        }

        if((Constant.DEPT_ROOT.equals(pidList)&&Constant.DEPT_ROOT.equals(deptIdList))){//最上级部门,查看中心表
            List<CenterDailyDTO> list = centerDailyService.list(params);
            finalList = ConvertUtils.sourceToTarget(list, SiteDailyDTO.class);
        }else{//下面医院，查看site表
            finalList = siteDailyService.list(params);
        }
        return finalList;
    }
}
